package com.meetflow.conference.controller;

import com.meetflow.common.annotation.Log;
import com.meetflow.common.core.controller.BaseController;
import com.meetflow.common.core.domain.AjaxResult;
import com.meetflow.common.core.domain.entity.SysUser;
import com.meetflow.common.core.page.TableDataInfo;
import com.meetflow.common.enums.BusinessType;
import com.meetflow.common.utils.DateUtils;
import com.meetflow.common.utils.SecurityUtils;
import com.meetflow.common.utils.poi.ExcelUtil;
import com.meetflow.conference.domain.MeetFlowAction;
import com.meetflow.conference.domain.MeetRecord;
import com.meetflow.conference.service.IMeetRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 我的申请Controller
 *
 * @author duanjj
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/conference/applyTask")
public class MeetRecordController extends BaseController {
    @Autowired
    private IMeetRecordService meetRecordService;

    /**
     * 查询我的申请列表
     */
    @PreAuthorize("@ss.hasPermi('conference:applyTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeetRecord meetRecord) {
        meetRecord.setApplicantUserId(SecurityUtils.getUserId());
        startPage();
        List<MeetRecord> list = meetRecordService.selectMeetRecordList(meetRecord);
        return getDataTable(list);
    }

    /**
     * 导出我的申请列表
     */
    @PreAuthorize("@ss.hasPermi('conference:applyTask:export')")
    @Log(title = "我的申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeetRecord meetRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setUserLevel(user.getUserLevel());
        List<MeetRecord> list = meetRecordService.selectExportMeetRecordList(meetRecord);
        ExcelUtil<MeetRecord> util = new ExcelUtil<MeetRecord>(MeetRecord.class);
        util.exportExcel(response, list, "我的申请数据");
    }

    /**
     * 获取我的申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('conference:applyTask:query')")
    @GetMapping(value = "/{id}/{taskStatus}")
    public AjaxResult getInfo(@PathVariable("id") Long id, @PathVariable("taskStatus") String taskStatus) {
        return success(meetRecordService.selectMeetRecordById(id, taskStatus));
    }

    /**
     * 新增我的申请
     */
    @PreAuthorize("@ss.hasPermi('conference:applyTask:add')")
    @Log(title = "我的申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeetRecord meetRecord) {
        meetRecord.setSerialNumber(DateUtils.dateTimeNow());
        return toAjax(meetRecordService.insertMeetRecord(meetRecord));
    }

    @PreAuthorize("@ss.hasPermi('conference:applyTask:add')")
    @Log(title = "我的申请-暂存", businessType = BusinessType.INSERT)
    @PostMapping("/stagingApplyTask")
    public AjaxResult stagingApplyTask(@RequestBody MeetRecord meetRecord) {
        meetRecord.setSerialNumber(DateUtils.dateTimeNow());
        return toAjax(meetRecordService.stagingApplyTask(meetRecord));
    }

    /**
     * 修改我的申请
     */
    @PreAuthorize("@ss.hasPermi('conference:applyTask:edit')")
    @Log(title = "我的申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeetRecord meetRecord) {
        return toAjax(meetRecordService.updateMeetRecord(meetRecord));
    }

    /**
     * 删除我的申请
     */
    @PreAuthorize("@ss.hasPermi('conference:applyTask:remove')")
    @Log(title = "我的申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(meetRecordService.deleteMeetRecordByIds(ids));
    }
}
